<template>
  <div>
    <!-- teleport 插入到指定位置 -->
    <teleport to="body">
      <!-- 遮罩层 -->
      <transition name="fade">
        <div
          @click="handleMask"
          v-if="modelValue"
          class="w-screen h-screen bg-zinc-900/80 z-40 fixed top-0 left-0"
        ></div>
      </transition>
      <transition name="popup-down-up">
        <div v-if="modelValue" class="w-screen bg-white z-50 fixed bottom-0">
          <slot> </slot>
        </div>
      </transition>
    </teleport>
  </div>
</template>

<script setup>
import { useScrollLock, useVModels } from "@vueuse/core";
import { watch } from "vue-demi";
// 接受父组件传递的参数 需要用props接受 方便下边使用,dom上绑定 直接绑定变量
// props下的属性不可直接更改
const props = defineProps({
  // modelValue 关键字 父组件可以直接使用v-model
  modelValue: {
    require: true,
    type: Boolean,
  },
});
// useVModels 可以把props做成响应式数据,直接修改
// const isVisible = useVModels(props);
// 发布事件 相当于 this.$emit
const emits = defineEmits(["update:modelValue", false]);
// 点击遮罩层关闭弹框 修改v-model
const handleMask = () => {
  emits("update:modelValue", false);
  // isVisible.modelValue.value = false;
  // console.log("isVisible :>> ", isVisible);
};
// 弹出层出现 锁定页面滚动 (响应式数据 直接修改isLocked 就可以锁定或者解锁页面滚动)
const isLocked = useScrollLock(document.body);
// 监听弹出层弹出动态修改页面是否可以滚动
watch(
  () => props.modelValue,
  (val) => {
    isLocked.value = val;
  },
  {
    immediate: true,
  }
);
</script>

<style lang="scss" scoped>
.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.popup-down-up-enter-active,
.popup-down-up-leave-active {
  transition: all 0.3s;
}
.popup-down-up-enter-from,
.popup-down-up-leave-to {
  transform: translateY(100%);
}
</style>